﻿function VideoInit(videoStreamsId) {
    var streams = $('#' + videoStreamsId).val().split(';');
    $.each(streams, function (i, v) {
        AddStream(v.split(','));
    });

    $('#content form').submit(function () {
        var result = true;
        var streams = '';
        $('#VideoStreamsContainer div').each(function (i) {
            $(this).find('input').each(function (i) {
                var v = $(this).val();
                if (v > 0) {
                    streams += v + ',';
                    $(this).removeClass('input-validation-error');
                }
                else {
                    $(this).addClass('input-validation-error').focus();
                    result = false;
                }
            });
            streams = streams.substring(0, streams.length - 1) + ';';
        });
        streams = streams.substring(0, streams.length - 1);
        if (result)
            $('#' + videoStreamsId).val(streams);
        return result;
    });
}

function AddStream(info) {
    if (info == null) info = [640, 360, 1200];
    var i = $('#VideoStreamsContainer div').length;
    var container = $('<div />');
    $('<span>' + (i + 1) + '</span>').appendTo(container);
    $('<input name="Width' + i + '" class="text" id="Width' + i + '" type="text" value="' + info[0] + '"/>').appendTo(container);
    $('<input name="Height' + i + '" class="text" id="Height' + i + '" type="text" value="' + info[1] + '"/>').appendTo(container);
    $('<input name="Bitrate' + i + '" class="text" id="Bitrate' + i + '" type="text" value="' + info[2] + '"/>').appendTo(container);
    $('<a href="javascript:function(){return false;}">x</a>').click(function () { RemoveStream(this); }).appendTo(container);
    $('#VideoStreamsContainer').append(container);
}

function RemoveStream(el) {
    if ($('#VideoStreamsContainer div').length > 1) {
        $(el).closest('div').remove();
        $('#VideoStreamsContainer div span:first-child').each(function (i) { $(this).text(i + 1) });
    }
}